﻿using famails.bus;
using FAMailUtils;
using FAMailUtils.Data.DAO;
using FAMailUtils.Data.DTO;
using FAMailUtils.Payment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin
{
    public partial class Confirmation : System.Web.UI.Page
    {
        private OnePayHandling oh = new OnePayHandling();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    string trasctiontype = Request.QueryString["paymentType"];
                    OnePayResponse opr = new OnePayResponse();

                    if (trasctiontype == "inland")
                    {
                        opr = oh.Response(true, Request.QueryString);
                    }
                    else
                    {
                        opr = oh.Response(false, Request.QueryString);
                    }

                    Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod() + "-HashValidateResult:" + opr.HashValidateResult, null, Common.logType.info);
                    if (opr.HashValidateResult != "INVALIDATED")
                    {
                        string code = Request.QueryString["vpc_TxnResponseCode"];
                        string transactioncode = Request.QueryString["vpc_OrderInfo"];
                        string receiverId = Request.QueryString["receiverId"];
                        string logs = "vpc_TxnResponseCode:{0}; vpc_OrderInfo:{1}; trasctiontype:{2}; receiverId:{3}";
                        logs = string.Format(logs, code, transactioncode, trasctiontype, receiverId);
                        Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod() + "-" + logs, null, Common.logType.info);
                        if (code == "0")
                        {
                            lblStatus.Text = "Giao dịch thành công";
                            ClientBUS bus = new ClientBUS();
                            bus.UpdatePayment(receiverId);
                            btnSuccess.Visible = true;
                        }
                        else
                        {
                            lblStatus.Text = "Giao dịch thất bại";
                            lblMessage.Text = FAMailUtils.Payment.OnePayHandling.OnePayErrorMessage(code);
                        }
                        PaymentDAO dao = new PaymentDAO();
                        PaymentDTO dto = dao.GetByID(receiverId.Split('-')[2]);
                        if (dto.receiverId != "")
                        {
                            dto.TxnResponseCode = code;
                            dto.TxnResponseCodeText = FAMailUtils.Payment.OnePayHandling.OnePayErrorMessage(code);
                            dao.Confirm(dto);
                        }
                        
                    }
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "Giao dịch thất bại!<br/>Vui lòng liên hệ với chúng tôi để được hỗ trợ thêm";
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }
    }
}